/** 
 * CanvasControlPane.java
 * @author Lex
 * 2007-8-25 ����11:13:52
 */
package fun.ui.canvas.control;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fun.configuration.CK;
import fun.configuration.Config;
import fun.ui.canvas.CanvasPane;

public class ColorsPane extends JPanel {
	private JPanel colorPane = null;
	private JLabel foregroundLabel = null;
	private JButton foregroundButton = null;
	private JLabel backgroundLabel = null;
	private JButton backgroundButton = null;
	private JLabel coordinatesLabel = null;
	private JButton coordinatesButton = null;
	private JPanel buttonsPane = null;
	private JPanel gluePane = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	// =================================
	private CanvasPane canvas = null;

	public ColorsPane(CanvasPane canvas) {
		super();
		this.canvas = canvas;
		initialize();
	}

	/*
	 * XXX JColorChooser �롡JInternalFrame ֮��������
	 */
	private Color showColorChooser(String title, Color color) {
		return JColorChooser.showDialog(this, title, color);
	}

	private void saveConfig(String key, Object value) {
		Config.instance().putProperty(key, value);
	}

	private void initialize() {
		GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
		gridBagConstraints41.gridx = 0;
		gridBagConstraints41.fill = GridBagConstraints.BOTH;
		gridBagConstraints41.weightx = 1.0;
		gridBagConstraints41.weighty = 1.0;
		gridBagConstraints41.gridy = 1;
		GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
		gridBagConstraints31.gridx = 0;
		gridBagConstraints31.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints31.weightx = 10.0;
		gridBagConstraints31.gridy = 2;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 10.0;
		gridBagConstraints.anchor = GridBagConstraints.CENTER;
		gridBagConstraints.gridy = 0;
		this.setSize(300, 200);
		this.setLayout(new GridBagLayout());
		this.add(getColorPane(), gridBagConstraints);
		this.add(getGluePane(), gridBagConstraints41);
		this.add(getButtonsPane(), gridBagConstraints31);
	}

	private JPanel getColorPane() {
		if (colorPane == null) {
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 1;
			gridBagConstraints12.insets = new Insets(0, 11, 5, 5);
			gridBagConstraints12.fill = GridBagConstraints.BOTH;
			gridBagConstraints12.gridy = 2;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.anchor = GridBagConstraints.EAST;
			gridBagConstraints10.insets = new Insets(0, 6, 5, 0);
			gridBagConstraints10.gridy = 2;
			coordinatesLabel = new JLabel();
			coordinatesLabel.setText("Coordinates");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.weighty = 1.0;
			gridBagConstraints5.gridheight = 3;
			gridBagConstraints5.gridy = 0;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.anchor = GridBagConstraints.EAST;
			gridBagConstraints4.insets = new Insets(0, 6, 5, 0);
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.insets = new Insets(0, 11, 5, 5);
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridy = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = GridBagConstraints.EAST;
			gridBagConstraints2.insets = new Insets(0, 6, 5, 0);
			gridBagConstraints2.gridy = 1;
			backgroundLabel = new JLabel();
			backgroundLabel.setText("Background");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.insets = new Insets(0, 11, 5, 5);
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			foregroundLabel = new JLabel();
			foregroundLabel.setText("Foreground");
			colorPane = new JPanel();
			colorPane.setLayout(new GridBagLayout());
			colorPane.setBorder(BorderFactory.createTitledBorder("Colors"));
			colorPane.add(foregroundLabel, gridBagConstraints4);
			colorPane.add(getForegroundButton(), gridBagConstraints1);
			colorPane.add(backgroundLabel, gridBagConstraints2);
			colorPane.add(getBackgroundButton(), gridBagConstraints3);
			colorPane.add(coordinatesLabel, gridBagConstraints10);
			colorPane.add(getCoordinatesButton(), gridBagConstraints12);
			colorPane.add(Box.createGlue(), gridBagConstraints5);
		}
		return colorPane;
	}

	private JButton getForegroundButton() {
		if (foregroundButton == null) {
			foregroundButton = new JButton();
			foregroundButton.setBackground(canvas.getForeground());
			foregroundButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Color c = showColorChooser("Foreground", canvas.getForeground());
					if (null != c) {
						canvas.setForeground(c);
						foregroundButton.setBackground(c);
						saveConfig(CK.UI_FUNCTION_FORE_COLOR, c.getRGB());
					}
				}
			});
		}
		return foregroundButton;
	}

	private JButton getBackgroundButton() {
		if (backgroundButton == null) {
			backgroundButton = new JButton();
			backgroundButton.setBackground(canvas.getBackground());
			backgroundButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Color c = showColorChooser("Background", canvas.getBackground());
					if (null != c) {
						canvas.setBackground(c);
						backgroundButton.setBackground(c);
						saveConfig(CK.UI_FUNCTION_BACK_COLOR, c.getRGB());
					}
				}
			});
		}
		return backgroundButton;
	}

	private JButton getCoordinatesButton() {
		if (coordinatesButton == null) {
			coordinatesButton = new JButton();
			coordinatesButton.setBackground(canvas.getCoordinate());
			coordinatesButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Color c = showColorChooser("Coordinates", canvas.getCoordinate());
					if (null != c) {
						canvas.setCoordinate(c);
						coordinatesButton.setBackground(c);
						saveConfig(CK.UI_FUNCTION_COORDINATES_COLOR, c.getRGB());
					}
				}
			});
		}
		return coordinatesButton;
	}

	private JPanel getButtonsPane() {
		if (buttonsPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setVgap(11);
			buttonsPane = new JPanel();
			buttonsPane.setLayout(flowLayout);
			buttonsPane.add(getOkButton(), null);
			buttonsPane.add(getCancelButton(), null);
		}
		return buttonsPane;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
			gluePane.setLayout(new GridBagLayout());
		}
		return gluePane;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
		}
		return cancelButton;
	}
}
